/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.client.runinspring.RunInSpringUtil;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;
import com.tmall.testgen.plugin.wizards.runinspring.RunInSpringWizadPage;
//456
public class TreeSelectListener implements SelectionListener{
	Menu menu;
	TreeEditor treeEditor;
	TreeViewer resultTreeViewer;
	Tree tree ;
	RunInSpringWizadPage page ;
	
	public TreeSelectListener(Menu menu,TreeEditor treeEditor,TreeViewer resultTreeViewer,Tree tree,RunInSpringWizadPage page ){
		this.menu=menu;
		this.treeEditor=treeEditor;
		this.resultTreeViewer=resultTreeViewer;
		this.tree=tree;
		this.page=page;
	}
	@Override
	public void widgetSelected(SelectionEvent e) {
		
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		Control oldEditor = treeEditor.getEditor();
		if (oldEditor != null) {
			oldEditor.dispose();
		}
		final TreeItem item = (TreeItem) e.item;
		if (item == null) {
			return;
		}
		Object obj = item.getData();

		
		final SpringTree currentTree = (SpringTree) obj;
		if (!RunInSpringUtil.isBasicType(currentTree.getClazz())) {
			currentTree.addFiledName();
			resultTreeViewer.refresh();
			return;
		}
		
		if(!currentTree.isEditable())
			return;
		
		if(!RunInSpringHandler.getInstance().isSpringStart()){
		    MyMessageDialog.openWarnDialog(page.getShell(), "请先启动Spring!");
		    return;
		}

		handleEditTree(tree, currentTree, item, treeEditor);
		//setPageComplete(false);
	}
	
	
	/**
	 * 处理叶子节点的编辑
	 * 
	 * @param tree
	 * @param currentTree
	 * @param item
	 * @param treeEditor
	 */
	protected void handleEditTree(Tree tree, final SpringTree currentTree,
			final TreeItem item, final TreeEditor treeEditor) {
		Text newEditor = new Text(tree, SWT.NONE);
		if(currentTree.getValue()==null)
			newEditor.setText("");
		else{
		    if(currentTree.getValue()!=null){
		        if(currentTree.getValue() instanceof Date){
		            DateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	                newEditor.setText( sf2.format(currentTree.getValue()));
		        }else if(currentTree.getValue() instanceof BigDecimal){
		            BigDecimal tem=(BigDecimal) currentTree.getValue();
		            newEditor.setText(tem.doubleValue()+"");
		        }
		       
		    }else{
		        newEditor.setText(currentTree.getValue()+"");
		    }
		}
		//newEditor.setText(currentTree.getValue());
		newEditor.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				Text text = (Text) treeEditor.getEditor();
				try {
                    currentTree.setValue(RunInSpringUtil.getValueByClass(currentTree.getClazz(), text.getText()));
                    
                    if(currentTree.getValue()!=null){
                        treeEditor.getItem().setText(currentTree.getClazz().getSimpleName() + " "+ currentTree.getFieldName() + "="+ text.getText());
                    }else{
                        treeEditor.getItem().setText(currentTree.getClazz().getSimpleName() + " "+ currentTree.getFieldName() );
                    }
                } catch (Exception e1) {
                }
				
				
			}
		});
		
		
		newEditor.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				Control oldEditor = treeEditor.getEditor();
				if (oldEditor != null) {
					//setPageComplete(true);
					oldEditor.dispose();
				}
				// treeEditor.setEditor(oldEditor,item);
			}

			@Override
			public void focusGained(FocusEvent e) {

			}
		});
		newEditor.selectAll();
		newEditor.setFocus();
		
		treeEditor.setEditor(newEditor, item);
	}

}
